package com.huqs.smoothphotoviewer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.huqs.smoothphotoview.library.fragment.PhotoFragment;
import com.huqs.smoothphotoview.library.ui.PhotoActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private GridView mGridView;
    private View mTempView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        this.setTitle(getString(R.string.app_name));
        mGridView = (GridView) findViewById(R.id.multi_photo_grid);
        imageView = (ImageView) findViewById(R.id.img_view);
        final ArrayList<String> datas = new ArrayList<String>();
        datas.clear();
        for (int i = 0; i < BitmapSampleUtil.IMAGES.length; i++) {
            datas.add(BitmapSampleUtil.IMAGES[i]);
        }
        final ImagesInnerGridViewAdapter adapter = new ImagesInnerGridViewAdapter(datas);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                ArrayList<Rect> rects = new ArrayList<>();
                for (int i = 0; i < datas.size(); i++) {
                    view = adapterView.getChildAt(i);
                    if (view == null) view = mTempView;
                    Rect rect = new Rect();
                    view.getGlobalVisibleRect(rect);
                    rects.add(rect);
                    mTempView = view;
                }
                Intent intent = new Intent(MainActivity.this, PhotoActivity.class);
                String imageArray[] = new String[datas.size()];
                for (int i = 0; i < imageArray.length; i++) {
                    imageArray[i] = datas.get(i);
                }
                intent.putExtra("imgUrls", imageArray);
                intent.putExtra("index", position);
                intent.putExtra("bounds", rects);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });
        final long startTime=new Date().getTime();
        Observable.create(new ObservableOnSubscribe<GlideDrawable>() {
            @Override
            public void subscribe(ObservableEmitter<GlideDrawable> e) throws Exception {
                GlideDrawable glideDrawable = Glide.with(MainActivity.this)
                        .load("http://images.china.cn/attachement/jpg/site1000/20121114/001aa0ba5c77120d2cd45d.jpg")
                        .into(100, 200)
                        .get();
                e.onNext(glideDrawable);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<GlideDrawable>() {
            @Override
            public void accept(GlideDrawable glideDrawable) throws Exception {
               String  filePath = Environment.getExternalStorageDirectory()+"/test.mp4";
                Glide.with(MainActivity.this).load(Uri.fromFile( new File( filePath ) )).error(glideDrawable).into(imageView);


            }
        });
    }


    private class ImagesInnerGridViewAdapter extends BaseAdapter {
        private List<String> datas;

        public ImagesInnerGridViewAdapter(List<String> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final SquareCenterImageView imageView = new SquareCenterImageView(MainActivity.this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ScaleType.CENTER_CROP);
            Glide.with(MainActivity.this).load(datas.get(position)).asBitmap().centerCrop().into(imageView);
            return imageView;
        }

    }
}
